package org.tuestudent.grouppa;

import java.util.ArrayList;

import org.tuestudent.grouppa.ChangePinPopup.OnPinChangeListener;
import org.tuestudent.grouppa.GroupViewController.ChangeGroupPinResponse;
import org.tuestudent.grouppa.GroupViewController.GroupMember;
import org.tuestudent.grouppa.GroupViewController.GetAllMembersResponse;
import org.tuestudent.grouppa.LoginViewController.GetProfileResponse;
import org.w3c.dom.Text;

import android.app.Activity;
import android.app.ActionBar;
import android.app.AlertDialog;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.os.Build;

public class GroupInfo extends Fragment implements OnClickListener, OnPinChangeListener {

    // Declaration of variables
    GrouppaScreen grouppaScreen;
    String groupName;
    ListView groupMembersList;
    ArrayAdapter<String> groupMembersAdapter;
    ArrayList<String> groupMemberArray;
    Button changePinButton;

    /**
     * open a popup with an editable textfield, containing the current group
     * information
     * 
     * This functionality is not yet implemented
     */
    private void editGroupInfoPopup() {
        // TODO:create popup
        // TODO:create editable textfield
        // TODO:load the current groupinfo in the editable textfield
        // TODO:create close button
        // TODO:create confirm button
    }

    /**
     * updates the group info
     * 
     * @param info
     *            the String containing the updated group info
     * @modifies groupInfo
     * 
     * This functionality is not yet implemented
     */
    private void editGroupInfo(String info) {
        // TODO:create code that updates the group info with String info
    }

    /**
     * create the addMemberPopup
     */
    private void addMemberPopup() {
        // TODO:create popup
        // TODO:create close button
        // TODO:create confirm button
        // TODO:create textfield to add userID's
    }

    /**
     * add a member to the group
     * 
     * @param userID
     *            the ID of the to be added user         
     */
    private void addMember(String userID) {
        addedMemberPopup(userID);
    }

    /**
     * popup to confirm the member userID has been added to the group
     * 
     * @param userID
     *            the ID of the added member
     *            
     * This functionality is not yet implemented           
     */
    private void addedMemberPopup(String userID) {
        // TODO:Create popup
        // TODO:Create 'OK' button to confirm and close the popup
    }

    /**
     * remove a member from the group
     * 
     * @param userID
     *            the ID of the to be removed user
     *            
     * This functionality is not yet implemented           
     */
    private void removeMember(String userID) {
        removedMemberPopup(userID);
    }

    /**
     * popup to confirm that member userID has been removed
     * 
     * @param userID
     *            the userID of the removed member
     *            
     * This functionality is not yet implemented           
     */
    private void removedMemberPopup(String userID) {
        // TODO:Create popup
        // TODO:Create 'OK' button to confirm and close the popup
    }

    /**
     * view information of the selected member
     * 
     * @param userID
     *            the ID of the member
     *            
     * This functionality is not yet implemented
     */
    private void viewMember(String userID) {
        // TODO:create popup
        // TODO:create close button
        // TODO:display member info (Nickname, email, phone, extra user info)
    }


    
    // ----------------onCreateView for the GroupInfo Fragment-------------------
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        // Inflate the view to its container
        View groupInfo =
                inflater.inflate(R.layout.fragment_group_info, container, false);
        RelativeLayout mainLayout =
                (RelativeLayout) groupInfo
                        .findViewById(R.id.containerGroupInfo);
        mainLayout.setOnTouchListener(grouppaScreen);
        
        // Retrieve and set the group name selected by the user
        groupName = Main.getContext().getCurrentGroup();
        TextView tvName = (TextView) groupInfo.findViewById(R.id.groupName);
        tvName.setText(groupName);
        
        // Change the pin from the group
        changePinButton = (Button) groupInfo.findViewById(R.id.groupInfoChangePinButton);
        changePinButton.setOnClickListener(this);
        
        setHasOptionsMenu(true);
        
        // Set the names of all group members
        groupMembersList = (ListView) groupInfo.findViewById(R.id.groupInfoMembersListView);
        groupMemberArray = new ArrayList<String>();
        groupMembersAdapter = new ArrayAdapter<String>(getActivity(),R.layout.group_info_member_text_view, groupMemberArray);
        groupMembersList.setAdapter(groupMembersAdapter);

        return groupInfo;
    }

    @Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
        grouppaScreen = Main.getContext().grouppaActivity;
    }

    // Method executed when starting the fragment
    @Override
    public void onStart() {
        super.onStart();
        AppContext ctx = Main.getContext();
        ctx.currentGrouppaFragment = this;
        GroupViewController GVC = GroupViewController.getInstance();
        ctx.setCurrentViewController(GVC);
        GVC.getGroupMembers(ctx.getCurrentGroup());
    }

    /**
     * list of members of currently displayed group
     * filled in onGetGroupMembersResponse
     */
    GroupMember []groupMembers;
    
    /**
     * Response of server on get all members request
     * @param response
     */
    public void onGetGroupMembersResponse(GetAllMembersResponse response) {
        //CHECK: Reload the list of members
        groupMemberArray.clear();
        groupMembers = response.members;
        for(GroupMember member : response.members)
        {
            String text = member.name;
            if(member.isManager)
                text = text+" [Manager]";
            groupMemberArray.add(text);
        }
        groupMembersAdapter.notifyDataSetChanged();
    }

    ChangePinPopup changePinPopup;
    @Override
    public void onClick(View v) {
        if(v== changePinButton)
        {
            changePinPopup = new ChangePinPopup(Main.getContext().getCurrentGroupPin(), this);
            changePinPopup.show(getFragmentManager(), "changeGroupPin");
        }
    }

    /**
     * called when pin change request to server finished
     * @param response
     */
    public void onPinChanged(ChangeGroupPinResponse response) {
        grouppaScreen.turnOffDialogs();
        changePinPopup.dismiss();
        if(response.succeeded)
        {
            grouppaScreen.actionSuccess("Group pin changed!");
        }
        else
        {
            grouppaScreen.errorPopup(response.errorReason);
        }
    }

    /**
     * Called when pin change popup finished
     */
    @Override
    public void pinChange(String newPin, ChangePinPopup popup) {
        GroupViewController.getInstance().changeGroupPin(Main.getContext().getCurrentGroup(), newPin);
        grouppaScreen.busyMessage("Changing group pin...");
        
    }
}
